﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SachOnlineLibrary.DAL;
using SachOnlineLibrary.Public;
using System.Data;

namespace SachOnlineLibrary.BLL
{
    public class PhieuDatHangBLL
    {

        private PhieuDatHang _phieudathang = new PhieuDatHang();
        PhieuDatHangDAL phieudathangdal = new PhieuDatHangDAL();
        ChiTietDatHangDAL chitietdathangdal = new ChiTietDatHangDAL();
        TaiKhoanDAL taikhoandal = new TaiKhoanDAL();
        SachDAL sachdal = new SachDAL();
       

        public bool ThemPhieuDatHang(int tkid, string ten, string sdt, string diachi, List<Sach> list)
        {
            try
            {
                phieudathangdal.Them_PhieuDatHang(tkid, ten, sdt, diachi);
                int maphieu = phieudathangdal.LayIDMax();
                foreach (Sach item in list)
                {
                    chitietdathangdal.Them_ChiTietDatHang(item.MaSach, maphieu, item.SoLuong, item.GiaTien);
                }
                return true;    
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                return false;
            }
        }

        public TaiKhoan LayTaiKhoan(string tentk)
        {
            TaiKhoan tk = new TaiKhoan();
            tk.ChuyenDoi(taikhoandal.LayTaiKhoanTheoTen(tentk).Rows[0]);
            return tk;
        }
        private DataTable _table = new DataTable();

        public DataTable Table
        {
            get { return _table; }
            set { _table = value; }
        }
        public string LaytenSP()
        {

            string name = Table.Rows[0]["TenSach"].ToString();
            return name;
        }

        public int LayGiaSP()
        {
            // int gia = (int)Table.Rows[0]["GiaTien"] ;
            int gia = int.Parse(Table.Rows[0]["giatien"].ToString());
            return gia;
        }
        public Sach LaySach(int masach)
        {
            Sach sach = new Sach();
            sach.ChuyenDoi(sachdal.LietKeSach(masach).Rows[0]);
            return sach;

        }
    }
}
